<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Dics;

use App\Goods;
use Illuminate\Support\Facades\Auth;

class GoodsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $goods = Goods::orderBy('updated_at', 'desc')->paginate(15);

        return view('system.goods.index', compact('goods'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $dicStyle = Dics::where("parent_id", "38")->get();

        $dicPostion = Dics::where("parent_id", "42")->get();

        $dicStyle_up = Dics::where("parent_id", "9")->get();
        $dicStyle_down = Dics::where("parent_id", "16")->get();

        $dicColor = Dics::where("parent_id", "2")->get();

        $dicSize_up = Dics::where("parent_id", "21")->get();
        $dicSize_down = Dics::where("parent_id", "28")->get();

        return view('system.goods.create', compact(['dicStyle', 'dicPostion', 'dicStyle_up', 'dicStyle_down', 'dicColor', 'dicSize_up', 'dicSize_down']));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'serial' => 'required|max:255',
            'symbol' => 'required|max:255',
            'style' => 'max:255',
            'postion' => 'max:255',
            'name' => 'max:255',
            'category' => 'required|max:255',
            'brand' => 'max:255',
            'color' => 'required|max:255',
            'size' => 'required|max:255',
            'number' => 'required|min:1',
            'price' => 'required|min:1',
            'state' => 'max:255',
            'remark' => 'max:255',
            'picurl' => 'required|image',
        ]);

        $input = $request->all();

        $file = $request->file('picurl');
        if ($file != null) {
            $allowed_extensions = ["png", "jpg", "gif"];
            if ($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
                return \Redirect::to('system/goods/create')->withErrors('无效的图片文件,系统直接接受 JPG, GIF, PNG');
            }

            $destinationPath = base_path() . '/public/upload/images';
            $extension = $file->getClientOriginalExtension();
            $fileName = str_random(10) . '.' . $extension;
            $file->move($destinationPath, $fileName);

            $input['picurl'] = $fileName;
        }
        $input["user_id"] = Auth::user()->id;
        Goods::create($input);
        return redirect(url('system/goods'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $goods = Goods::where('serial', $id)->paginate(15);

        $dicStyle = Dics::where("parent_id", "38")->get();

        $dicPostion = Dics::where("parent_id", "42")->get();

        $dicStyle_up = Dics::where("parent_id", "9")->get();
        $dicStyle_down = Dics::where("parent_id", "16")->get();

        $dicColor = Dics::where("parent_id", "2")->get();

        $dicSize_up = Dics::where("parent_id", "21")->get();
        $dicSize_down = Dics::where("parent_id", "28")->get();

        return view('system.goods.index', compact(['goods', 'dicStyle', 'dicPostion', 'dicStyle_up', 'dicStyle_down', 'dicColor', 'dicSize_up', 'dicSize_down']));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $goods = Goods::find($id);
        $goods->delete();
    }

    public static function calcPrice($price)
    {
        if ($price <= 20) {
            $price += 79;
        } else {
            $price += 120;
        }

        return $price;
    }

    public static function calcDiscount($price,$serial)
    {
        if(substr($serial,0,2)==="01"){
            if ($price <= 20) {
                $price += 49;
            } else {
                $price += 75;
            }
        }else{
            $price += 85;
        }
        return $price;
    }
}
